package com.example.smsquiz;

import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Vector;

import android.telephony.SmsManager;

public class AppModel {
	
	private static AppModel instance;
	
	private Queue<SmsDTO> messages;
	private TextDbProvider dbProvider;
    private Boolean isAddQuestionProcessing = false;
	
	private AppModel(){
		messages = new LinkedList<SmsDTO>();
		AppLogger.Write("Sms Stack created");
	}
	
	public void initialize(String dbPath){
		dbProvider = new TextDbProvider(dbPath);
	}
	
	public static AppModel getInstance(){
		if(instance == null){
			instance = new AppModel();
		}
		return instance;
	}
	
	public void addMessage(SmsDTO message){
		messages.add(message);
	}
	
	public SmsDTO getMessage() {
		return messages.poll();
	}
	
	public String getAnswerOnSimilarQuestion(String question){
		if(dbProvider != null){
			try {
				return dbProvider.getAnswerOnSimilarQuestion(question);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				AppLogger.WriteError(e);
			}
		}
		return null;
	}
	
	public void sendSms(String phone, String message){
		try{
			SmsManager smsManager = SmsManager.getDefault();
			smsManager.sendTextMessage(phone, null, message, null, null);
			AppLogger.Write("SmsManager send sms");
		}
		catch (Exception e) {
			AppLogger.WriteError(e);
		}
		
	}

    public Vector<String> getUrls(){
        Vector<String> mock = new Vector<String>();
        mock.add("google.com");
        mock.add("yandex.com");
        mock.add("vk.com");
        mock.add("polypore.biz");
        return mock;
    }

    public void addQuestion(String question, String answer){
        try {
            dbProvider.AddQuestion(question, answer);
        } catch (Exception e) {
            AppLogger.WriteError(e);
        }
    }

    public void setQuestionProcessing(Boolean value){
        isAddQuestionProcessing = value;
    }

    public Boolean getQuestionProcessing(){
        return isAddQuestionProcessing;
    }

    public Boolean isServiceActive(){
        return SmsQuizService.isActive();
    }
}
